<template>
  <Card>
    <Form :model="newspaperDetail" :label-width="150" ref="newspaperDetail" :rules="ruleValidate">
      <h3 class="form-tip">
        日报详情
      </h3>
      <Row>
        <FormItem label="标题：" prop="title">
          <Input style="width: 250px;" placeholder="请输入日报标题" v-model="newspaperDetail.title"/>
        </FormItem>
      </Row>
      <Row>
        <FormItem label="正文：" prop="content">
          <Input type="textarea" style="width: 850px;padding-right:100px" placeholder="请输入日报正文" v-model="newspaperDetail.content" :autosize="{minRows: 6,maxRows: 10}"/>
        </FormItem>
      </Row>
      <!--<Row >-->
        <!--<FormItem label="状态：" prop="status">-->
          <!--<RadioGroup  v-model="newspaperDetail.status" >-->
            <!--<Radio :disabled="!add && !!newspaperDetail.status" :label="0">未读</Radio>-->
            <!--<Radio :disabled="!add && !!newspaperDetail.status" :label="1">已读</Radio>-->
          <!--</RadioGroup>-->
        <!--</FormItem>-->
      <!--</Row>-->
      <Row>
        <FormItem>
          <Button type="primary" @click="save('newspaperDetail')">{{add?'新增':'修改'}}</Button>
        </FormItem>
      </Row>
    </Form>
  </Card>
</template>

<script>
  import { getDailyDetail, editDaily } from '@/api/newspaper'
  export default {
    name: 'newspaper-detail',
    data () {
      return {
        add: false,
        newspaperDetail:{
          status: 0
        },
        ruleValidate: {
          title: [
            {required: true, message: '日报标题不能为空', trigger: 'blur'}
          ],
          content: [
            {required: true, message: '日报内容不能为空', trigger: 'blur'}
          ]
        },
      }
    },
    methods: {
      getDetail (id) {
        getDailyDetail(id).then(data => {
          this.newspaperDetail = data
        })
      },
      save (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            if (this.add) {
              const params = Object.assign({},this.newspaperDetail,{adminId:parseInt(this.$store.state.user.userId)})
              editDaily(params).then(()=> {
                this.$Message.success('新增成功')
                this.$router.go(-1)
              })
            } else if (this.$store.state.user.userType === 2) {
              editDaily(this.newspaperDetail).then(()=> {
                this.$Message.success('修改成功')
                this.$router.go(-1)
              })
            } else {
              this.$Message.error('您暂无权限');
            }
          } else {
            this.$Message.error('保存失败');
          }
        });
      },

    },
    mounted () {
      if(this.$route.query.id) {
        this.getDetail(this.$route.query.id)
      } else {
        this.add = true
      }
    }
  }
</script>

<style scoped>

</style>
